/**
 * Advanced Software Engineering (Monica Farrow)
 * AdminGUI class
 * 
 * Some of the methods in this class have been based on lecture notes provided by
 * Monica Farrow as a part of Software Engineering courses at Heriot-Watt University.
 */

package depotAdmin;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * AdminGUI class providing access to the Stop button and speed up/down controls
 * @author Ionut Iordache, Nikky Afunugo, Dave Bond
 */
public class AdminGUI extends JFrame implements Runnable, ActionListener {

	private static final long serialVersionUID = 1L;
	// title panel
	JPanel titlePanel = new JPanel();
	JLabel guiTitle = new JLabel();  // GUI heading

	// stop panel
	JPanel stopPanel = new JPanel();
	JLabel stopTitle = new JLabel(); // holds panel heading
	JButton stopButton = new JButton(); // used to flag end of working day

	// speed panel
	JPanel speedPanel = new JPanel();
	JLabel speedTitle = new JLabel(); // holds panel heading
	JLabel currSpeed = new JLabel(); // holds current speed setting
	JButton speedPlusButton = new JButton(); // used to increase speed
	JButton speedMinusButton = new JButton(); // used to decrease speed


	/**
	 * run method - does all the work
	 */
	public void run() {

		// create and display CustomerGUI 
		createDisplay();

	}

	/**
	 * creates and starts the GUI
	 */
	public void createDisplay() {

		// set GUI titlebar
		this.setTitle("Admin Functions");

		// Sets location of GUI
		this.setLocation(220,420);

		// set GUI close button to confirm system exit
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		WindowAdapter exitListener = new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				int confirm = JOptionPane.showOptionDialog(null, "Are You Sure to Close Application?", "Exit Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
				if (confirm == 0) {
					System.exit(0);
				}
			}
		};
		this.addWindowListener(exitListener);

		// Set size of GUI
		this.setMinimumSize(new Dimension(300, 100));

		// set layout
		this.setLayout(new BorderLayout());

		// create & add title panel
		createTitlePanel();
		this.add(titlePanel, BorderLayout.NORTH);

		// create & add stop panel
		createStopPanel();
		this.add(stopPanel, BorderLayout.EAST);

		// create & add stop panel
		createSpeedPanel();
		this.add(speedPanel, BorderLayout.WEST);

		// Pack and make GUI visible
		this.pack();
		this.setVisible(true);
	}

	/**
	 *  Creates the title panel
	 */
	public void createTitlePanel() {

		titlePanel.setLayout(new FlowLayout());

		// create GUI header
		guiTitle.setText(" Admin Functions ");    

		// set font large and bold 
		guiTitle.setFont(new Font (Font.SANS_SERIF, Font.BOLD,14));

		// add light border
		guiTitle.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));

		guiTitle.setOpaque(true);

		// set background colour to white
		guiTitle.setBackground(Color.WHITE);

		// add it at the top of the title panel
		titlePanel.add(guiTitle); 
	}



	/**
	 *  Creates the customers queuetop button panel
	 */
	public void createStopPanel() {

		stopPanel.setLayout(new BorderLayout());
		stopPanel.setSize(100, 100);

		stopTitle.setText(" Finish processing: "); // set panel heading

		stopButton.setText("Stop work!"); // set text for button
		stopButton.setFocusPainted(false);
		stopButton.addActionListener(this); // registers button with ActionListener

		// inner panel for speed buttons
		JPanel innerStopPanel = new JPanel();
		innerStopPanel.setLayout(new FlowLayout());

		// add components to panel
		innerStopPanel.add(stopButton);
		stopPanel.add(stopTitle, BorderLayout.NORTH);
		stopPanel.add(innerStopPanel, BorderLayout.CENTER);

	} 


	/**
	 *  Creates the speed control panel
	 */
	public void createSpeedPanel() {

		speedPanel.setLayout(new BorderLayout());
		speedPanel.setSize(100, 100);

		speedTitle.setText(" Set processing speed: "); // set panel heading

		speedMinusButton.setText("<<"); // set text for button
		speedMinusButton.setFocusPainted(false);
		speedMinusButton.addActionListener(this); // registers button with ActionListener

		speedPlusButton.setText(">>"); // set text for button
		speedPlusButton.setFocusPainted(false);
		speedPlusButton.addActionListener(this); // registers button with ActionListener

		currSpeed.setText(" " + Integer.toString(DepotUtils.getSpeedLevel()) + " ");
		currSpeed.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1)); // add light border
		currSpeed.setOpaque(true);
		currSpeed.setBackground(Color.WHITE); // set background colour to white


		// inner panel for speed buttons
		JPanel innerSpeedPanel = new JPanel();
		innerSpeedPanel.setLayout(new FlowLayout());

		// add components to panel
		innerSpeedPanel.add(speedMinusButton);
		innerSpeedPanel.add(currSpeed);
		innerSpeedPanel.add(speedPlusButton);
		speedPanel.add(speedTitle, BorderLayout.NORTH);
		speedPanel.add(innerSpeedPanel, BorderLayout.CENTER);

	} 


	/**
	 *  performs various actions when events detected
	 *  @param e   the ActionEvent
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == stopButton){ // stop button pressed
			// set endOfDay variable to true
			// = stops main processing tasks and starts final tidy up
			DepotUtils.setEndOfDay(true);
		}

		else if (e.getSource() == speedMinusButton){ // speed up button pressed

			// decrement speedLevel variable
			DepotUtils.setSpeedLevelDown();
			// set speed level display to new value
			currSpeed.setText(" " + Integer.toString(DepotUtils.getSpeedLevel()) + " ");
		}

		else if (e.getSource() == speedPlusButton){ // slow down button pressed

			// increment speedLevel variable
			DepotUtils.setSpeedLevelUp();
			// set speed level display to new value
			currSpeed.setText(" " + Integer.toString(DepotUtils.getSpeedLevel()) + " ");
		}

	}

}